﻿using Camc.Quality.DataExporting.Excel.EpPlus;
using Camc.Quality.Dto;
using Camc.Quality.Storage;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Camc.Quality.ProductionControl
{
    public class StatisticalExcelAppService : EpPlusExcelExporterBase, IStatisticalWorkHours
    {

        private readonly ITempFileCacheManager _tempFileCacheManager;

        public StatisticalExcelAppService(ITempFileCacheManager tempFileCacheManager) : base(tempFileCacheManager)
        {
            _tempFileCacheManager = tempFileCacheManager;
        }


        public string ExportStatisticalOwnHour(List<ManHourOutput> Output) {
            var processesExplain = System.DateTime.Now.ToString("yyyyMMddHHmm");
            return CreateExcelPackageLocal(
            $"人员工时统计{processesExplain}.xlsx",
            excelPackage =>
            {
                var sheet = excelPackage.Workbook.Worksheets.Add(L("人员工时统计"));
                var tableHeader = new string[] { "人员", "任务编号", "型号", "发次", "规程编号", "规程名称", "工序图号", "工序名称", "工步内容", "工步名称", "报工时间", "标准工时", "提报工时" };
                sheet.OutLineApplyStyle = true;
                AddHeader(sheet, tableHeader.ToArray());
                AddObjects(
                    sheet, 2, Output,
                    _ => _.UserName,
                    _ => _.JobNumber,
                    _ => _.Model,
                    _ => _.LotNo,
                    _ => _.TechniqueNumber,
                    _ => _.TechniqueName,
                    _ => _.DrawingNo,
                    _ => _.ProductionName,
                    _ => _.WorkStepDrawingNo,
                    _ => _.WorkStepName,
                    _ => _.ReportTime,
                    _ => _.WorkingHour,
                    _ => _.ReportHour
                );
                for (var i = 1; i <= tableHeader.Count(); i++)
                {
                    sheet.Column(i).AutoFit();
                }
                var timeColumn = sheet.Column(11);
                timeColumn.Style.Numberformat.Format = "yyyy-mm-dd hh:mm:ss";
            });
        }

        public string ExportStatisticalGroupHour(List<ManGroupOutput> Output) {
            var processesExplain = System.DateTime.Now.ToString("yyyyMMddHHmm");
            return CreateExcelPackageLocal(
            $"班组工时统计{processesExplain}.xlsx",
            excelPackage =>
            {
                var sheet = excelPackage.Workbook.Worksheets.Add(L("班组工时统计"));
                var tableHeader = new string[] { "装配班组", "人员", "型号", "发次", "报工任务数", "标准总工时", "提报总工时" };
                sheet.OutLineApplyStyle = true;
                AddHeader(sheet, tableHeader.ToArray());
                AddObjects(
                    sheet, 2, Output,
                    _ => _.OrganizationUnitDesc,
                    _ => _.UserName,
                    _ => _.Model,
                    _ => _.LotNo,
                    _ => _.TaskConut,
                    _ => _.SumWorkingHour,
                    _ => _.SumReportHour

                );
                for (var i = 1; i <= tableHeader.Count(); i++)
                {
                    sheet.Column(i).AutoFit();
                }
            });
    }
    }
}
